package drds.binlog.parse.driver.packets.client;

import drds.binlog.common.Authors;
import drds.binlog.parse.driver.packets.CommandPacket;
import drds.binlog.parse.driver.utils.Bytes;
import drds.common.Author;
import org.apache.commons.lang.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * semi ack command
 */
@Author(name = Authors.LI_YANG)
public class SemiAckCommandPacket extends CommandPacket
{

    public long binlogPosition;
    public String binlogFileName;

    public SemiAckCommandPacket()
    {

    }

    @Override
    public void fromBytes(byte[] bytes) throws IOException
    {
    }

    /**
     * <pre>
     * Bytes                        Name
     *  --------------------------------------------------------
     *  Bytes                        Name
     *  -----                        ----
     *  1                            semi mark
     *  8                            binlog position to start at (little endian)
     *  n                            binlog file name
     *
     * </pre>
     */
    public byte[] toBytes() throws IOException
    {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        // 0 write semi mark
        byteArrayOutputStream.write(0xef);
        // 1 write 8 bytes for position
        Bytes.writeUnsigned8IntLittleEndian(byteArrayOutputStream, binlogPosition);

        // 2 write binlog filename
        if (StringUtils.isNotEmpty(binlogFileName))
        {
            byteArrayOutputStream.write(binlogFileName.getBytes());
        }
        return byteArrayOutputStream.toByteArray();
    }

}
